/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JIFRegistrarAluguel.java
 *
 * Created on 18/06/2011, 10:44:55
 */
package br.unesp.demac.bcc.soo.view.aluguel;

import br.unesp.demac.bcc.soo.bean.Aluguel;
import br.unesp.demac.bcc.soo.bean.AluguelRel;
import br.unesp.demac.bcc.soo.bean.Dvd;
import br.unesp.demac.bcc.soo.bean.Pessoa;
import br.unesp.demac.bcc.soo.dao.AluguelDAO;
import br.unesp.demac.bcc.soo.dao.AluguelDAOImp;
import br.unesp.demac.bcc.soo.dao.DvdDAO;
import br.unesp.demac.bcc.soo.dao.DvdDAOImp;
import br.unesp.demac.bcc.soo.dao.PessoaDAO;
import br.unesp.demac.bcc.soo.dao.PessoaDAOImp;
import br.unesp.demac.bcc.soo.util.Dialog;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author flavioam
 */
public class JIFRegistrarAluguel extends javax.swing.JInternalFrame {

    /** Creates new form JIFRegistrarAluguel */
    public JIFRegistrarAluguel() {
        initComponents();
        btnSalvaAlguel.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtCodCliente = new javax.swing.JTextField();
        txtNomeCliente = new javax.swing.JTextField();
        txtCpfCliente = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtTelResid = new javax.swing.JTextField();
        txtEnd = new javax.swing.JTextField();
        txtTelCel = new javax.swing.JTextField();
        btnProcurarCliente = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtCodDvd = new javax.swing.JTextField();
        btnProcurarDvd = new javax.swing.JButton();
        txtNomeDvd = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtModalidadeDvd = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtValorDvd = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtClassDvd = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtGeneroDvd = new javax.swing.JTextField();
        btnAdicionarAluguel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaAluguel = new javax.swing.JTable();
        btnDeletaLinha = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        txtValorTotal = new javax.swing.JTextField();
        btnCancelaAluguel = new javax.swing.JButton();
        btnSalvaAlguel = new javax.swing.JButton();

        setClosable(true);
        setTitle("Registro de Aluguel");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico16x16/outIcon.png"))); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cliente"));

        jLabel1.setText("Cód. Cliente:");

        jLabel2.setText("Nome:");

        jLabel3.setText("Cpf: ");

        txtNomeCliente.setEnabled(false);

        txtCpfCliente.setEnabled(false);

        jLabel4.setText("Tel. Resid.:");

        jLabel5.setText("Tel. Cel.:");

        jLabel6.setText("End.:");

        txtTelResid.setEnabled(false);
        txtTelResid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelResidActionPerformed(evt);
            }
        });

        txtEnd.setEnabled(false);

        txtTelCel.setEnabled(false);

        btnProcurarCliente.setText("Procurar");
        btnProcurarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcurarClienteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCodCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnProcurarCliente))
                    .addComponent(txtNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtEnd, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtTelResid, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtTelCel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)))))
                .addContainerGap(168, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtTelResid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnProcurarCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtTelCel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Aluguel"));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Dvd"));

        jLabel7.setText("Cód. Dvd:");

        jLabel8.setText("Nome:");

        txtCodDvd.setEnabled(false);
        txtCodDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodDvdActionPerformed(evt);
            }
        });

        btnProcurarDvd.setText("Procurar");
        btnProcurarDvd.setEnabled(false);
        btnProcurarDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcurarDvdActionPerformed(evt);
            }
        });

        txtNomeDvd.setEnabled(false);

        jLabel9.setText("Modalidade:");

        txtModalidadeDvd.setEnabled(false);

        jLabel10.setText("Valor:");

        txtValorDvd.setEnabled(false);

        jLabel11.setText("Class.:");

        txtClassDvd.setEnabled(false);
        txtClassDvd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtClassDvdActionPerformed(evt);
            }
        });

        jLabel12.setText("Gênero:");

        txtGeneroDvd.setEnabled(false);

        btnAdicionarAluguel.setText("Adicionar ao Aluguel");
        btnAdicionarAluguel.setEnabled(false);
        btnAdicionarAluguel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAdicionarAluguelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtModalidadeDvd)
                            .addComponent(txtNomeDvd, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtCodDvd, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnProcurarDvd)))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtGeneroDvd))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(18, 18, 18)
                        .addComponent(txtValorDvd))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtClassDvd, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
                .addComponent(btnAdicionarAluguel)
                .addGap(70, 70, 70))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addContainerGap(730, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtCodDvd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnProcurarDvd)
                    .addComponent(jLabel10)
                    .addComponent(txtValorDvd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(txtNomeDvd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtModalidadeDvd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel11)
                            .addComponent(txtClassDvd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAdicionarAluguel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(txtGeneroDvd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tabelaAluguel.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cód. Dvd", "Dvd", "Valor", "Data Devolução"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaAluguel.setEnabled(false);
        jScrollPane1.setViewportView(tabelaAluguel);

        btnDeletaLinha.setText("Deletar Linha Selecionada");
        btnDeletaLinha.setEnabled(false);
        btnDeletaLinha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletaLinhaActionPerformed(evt);
            }
        });

        jLabel13.setText("Valor Total: ");

        txtValorTotal.setText("0");
        txtValorTotal.setEnabled(false);
        txtValorTotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtValorTotalActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(455, 455, 455)
                .addComponent(btnDeletaLinha))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDeletaLinha))
                .addContainerGap())
        );

        btnCancelaAluguel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico48x48/cancel.png"))); // NOI18N
        btnCancelaAluguel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelaAluguelActionPerformed(evt);
            }
        });

        btnSalvaAlguel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/img/ico48x48/save.png"))); // NOI18N
        btnSalvaAlguel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvaAlguelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(344, 344, 344)
                        .addComponent(btnSalvaAlguel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelaAluguel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSalvaAlguel)
                    .addComponent(btnCancelaAluguel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnProcurarDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcurarDvdActionPerformed
        try {
            // TODO add your handling code here:

            DvdDAO dDao = new DvdDAOImp();
            Dvd d = dDao.selecionaDvdById(Integer.parseInt(txtCodDvd.getText()));

            if (d == null) {

                Dialog.errorDialog("Dvd não encontrado!");
            } else {

                txtClassDvd.setText(String.valueOf(d.getClassificacao()));
                txtGeneroDvd.setText(d.getGenero());
                txtModalidadeDvd.setText(d.getModalidade().getNome());
                txtNomeDvd.setText(d.getNome());
                txtValorDvd.setText(String.valueOf(d.getModalidade().getValorAluguel()));

                btnAdicionarAluguel.setEnabled(true);
                btnProcurarDvd.setEnabled(false);
                txtCodDvd.setEnabled(false);
            }
        } catch (Exception ex) {
            Dialog.errorDialog(ex.getMessage());
        }

    }//GEN-LAST:event_btnProcurarDvdActionPerformed

    private void btnAdicionarAluguelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAdicionarAluguelActionPerformed
        try {
            // TODO add your handling code here:
            DefaultTableModel tableModel = (DefaultTableModel) tabelaAluguel.getModel();

            int codDvd = Integer.parseInt(txtCodDvd.getText());

            DvdDAO dDao = new DvdDAOImp();

            Dvd d = dDao.selecionaDvdById(codDvd);

            GregorianCalendar gc = new GregorianCalendar();
            gc.add(GregorianCalendar.DAY_OF_MONTH, d.getModalidade().getDiasDevolucao());

            SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");

            String dataDevolucao = sd.format(gc.getTime());

            Object newRow[] = new Object[4];
            newRow[0] = d.getIdDvd();
            newRow[1] = d.getNome();
            newRow[2] = d.getModalidade().getValorAluguel();
            newRow[3] = dataDevolucao;

            tableModel.addRow(newRow);
            tabelaAluguel.setEnabled(true);

            double total = Double.parseDouble(txtValorTotal.getText());
            total += d.getModalidade().getValorAluguel();
            txtValorTotal.setText(String.valueOf(total));

            txtCodDvd.setText("");
            txtCodDvd.setEnabled(true);
            btnProcurarDvd.setEnabled(true);
            btnAdicionarAluguel.setEnabled(false);
            btnDeletaLinha.setEnabled(true);
            btnSalvaAlguel.setEnabled(true);

            txtClassDvd.setText("");
            txtGeneroDvd.setText("");
            txtModalidadeDvd.setText("");
            txtNomeDvd.setText("");
            txtValorDvd.setText("");
            txtCodDvd.requestFocus();
        } catch (Exception ex) {
            Dialog.errorDialog(ex.getMessage());
        }

    }//GEN-LAST:event_btnAdicionarAluguelActionPerformed

    private void btnDeletaLinhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletaLinhaActionPerformed
        // TODO add your handling code here:
        DefaultTableModel tableModel = (DefaultTableModel) tabelaAluguel.getModel();

        int linhaSelecionada = tabelaAluguel.getSelectedRow();

        if (linhaSelecionada < 0) {

            Dialog.errorDialog("Selecione uma linha para deletar!");
        } else {
            try {
                DvdDAO dDao = new DvdDAOImp();
                Dvd d = dDao.selecionaDvdById((Integer) tableModel.getValueAt(linhaSelecionada, 0));

                double total = Double.parseDouble(txtValorTotal.getText());
                total -= d.getModalidade().getValorAluguel();
                txtValorTotal.setText(String.valueOf(total));

                tableModel.removeRow(linhaSelecionada);

                if (tabelaAluguel.getRowCount() == 0) {

                    btnDeletaLinha.setEnabled(false);
                    btnSalvaAlguel.setEnabled(false);
                }
            } catch (Exception ex) {
                Dialog.errorDialog(ex.getMessage());
            }

        }
    }//GEN-LAST:event_btnDeletaLinhaActionPerformed

    private void txtTelResidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelResidActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTelResidActionPerformed

    private void txtCodDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodDvdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCodDvdActionPerformed

    private void txtClassDvdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtClassDvdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtClassDvdActionPerformed

    private void txtValorTotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtValorTotalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtValorTotalActionPerformed

    private void btnCancelaAluguelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelaAluguelActionPerformed
        // TODO add your handling code here:
        btnSalvaAlguel.setEnabled(false);
        btnProcurarDvd.setEnabled(false);
        btnProcurarCliente.setEnabled(true);
        txtCodCliente.setEnabled(true);
        txtCodDvd.setEnabled(false);

        txtClassDvd.setText("");
        txtCodCliente.setText("");
        txtCodDvd.setText("");
        txtCpfCliente.setText("");
        txtEnd.setText("");
        txtGeneroDvd.setText("");
        txtModalidadeDvd.setText("");
        txtNomeCliente.setText("");
        txtNomeDvd.setText("");
        txtTelCel.setText("");
        txtTelResid.setText("");
        txtValorDvd.setText("");
        txtValorTotal.setText("0");

        txtClassDvd.setEnabled(false);
        txtCpfCliente.setEnabled(false);
        txtEnd.setEnabled(false);
        txtGeneroDvd.setEnabled(false);
        txtModalidadeDvd.setEnabled(false);
        txtNomeCliente.setEnabled(false);
        txtNomeDvd.setEnabled(false);
        txtTelCel.setEnabled(false);
        txtTelResid.setEnabled(false);
        txtValorDvd.setEnabled(false);
        txtValorTotal.setEnabled(false);

        txtCodCliente.requestFocus();

        DefaultTableModel model = (DefaultTableModel) tabelaAluguel.getModel();
        model.setRowCount(0);

}//GEN-LAST:event_btnCancelaAluguelActionPerformed

    private void btnSalvaAlguelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvaAlguelActionPerformed
        // TODO add your handling code here:

        try {

            PessoaDAO pDao = new PessoaDAOImp();
            Pessoa p = pDao.selecionaClienteById(Integer.parseInt(txtCodCliente.getText()));

            DvdDAO dDao = new DvdDAOImp();

            Aluguel a = new Aluguel();
            a.setPessoa(p);
            a.setValor(Double.parseDouble(txtValorTotal.getText()));

            ArrayList<AluguelRel> l = new ArrayList<AluguelRel>();

            DefaultTableModel model = (DefaultTableModel) tabelaAluguel.getModel();
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

            for (int i = 0; i < model.getRowCount(); i++) {
                try {
                    AluguelRel aRel = new AluguelRel();

                    Dvd d = dDao.selecionaDvdById(Integer.parseInt(model.getValueAt(i, 0).toString()));
                    aRel.setDvd(d);
                    aRel.setDataDevolucao(sdf.parse((String) model.getValueAt(i, 3)));
                    aRel.setValor(Double.parseDouble(txtValorTotal.getText()));

                    l.add(aRel);

                } catch (ParseException ex) {
                    ex.printStackTrace();
                    Dialog.errorDialog(ex.getMessage());
                }
            }

            a.setAlguelRel(l);

            AluguelDAO aDao = new AluguelDAOImp();
            aDao.salvaAluguel(a);

            Dialog.sucessoDialog("Aluguel Registrado com Sucesso!");
            btnCancelaAluguel.doClick();

        } catch (Exception e) {

            Dialog.errorDialog(e.getMessage());
        }

}//GEN-LAST:event_btnSalvaAlguelActionPerformed

    private void btnProcurarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcurarClienteActionPerformed
        try {
            // TODO add your handling code here:
            PessoaDAO pDao = new PessoaDAOImp();
            Pessoa p = pDao.selecionaClienteById(Integer.parseInt(txtCodCliente.getText()));

            if (p == null) {

                Dialog.errorDialog("Cliente não encontrado!");

            } else {

                btnProcurarCliente.setEnabled(false);
                txtCodCliente.setEnabled(false);

                txtNomeCliente.setText(p.getNome());
                txtCpfCliente.setText(p.getCpf());
                txtEnd.setText(p.getEndereco().getEndereco());
                txtTelCel.setText(p.getContato().getTelefoneCelular());
                txtTelResid.setText(p.getContato().getTelefoneResidencial());

                txtCodDvd.setEnabled(true);
                btnProcurarDvd.setEnabled(true);
                txtCodDvd.requestFocus();


            }
        } catch (Exception ex) {
            Dialog.errorDialog(ex.getMessage());
        }
    }//GEN-LAST:event_btnProcurarClienteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdicionarAluguel;
    private javax.swing.JButton btnCancelaAluguel;
    private javax.swing.JButton btnDeletaLinha;
    private javax.swing.JButton btnProcurarCliente;
    private javax.swing.JButton btnProcurarDvd;
    private javax.swing.JButton btnSalvaAlguel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaAluguel;
    private javax.swing.JTextField txtClassDvd;
    private javax.swing.JTextField txtCodCliente;
    private javax.swing.JTextField txtCodDvd;
    private javax.swing.JTextField txtCpfCliente;
    private javax.swing.JTextField txtEnd;
    private javax.swing.JTextField txtGeneroDvd;
    private javax.swing.JTextField txtModalidadeDvd;
    private javax.swing.JTextField txtNomeCliente;
    private javax.swing.JTextField txtNomeDvd;
    private javax.swing.JTextField txtTelCel;
    private javax.swing.JTextField txtTelResid;
    private javax.swing.JTextField txtValorDvd;
    private javax.swing.JTextField txtValorTotal;
    // End of variables declaration//GEN-END:variables
}
